package by.cooker.persdic;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
 * @author cooker
 */
public class PersDic {

    public static final String NAME = "PersDic";
    public static final String VERSION = "0.1.1";
    
    private static final String PROPERTIES_FILE_NAME = "persdic.properties";
    
    private static Properties globalProps;
    
    public static void loadProperties(File propsFile) {
	try {
	    globalProps = new Properties();
	    FileInputStream fos = new FileInputStream(propsFile);
	    globalProps.load(fos);
	    fos.close();
	} catch (FileNotFoundException e) {
	    System.err.println("Properties file " + propsFile + " not found.");
	    System.exit(1);
	} catch (IOException e) {
	    System.err.println("Problem with reading properties file.");
	    System.exit(1);
	}
    }

    public static String getString(String key) {
	if (key == null)
	    throw new IllegalArgumentException("Key=" + key);
	String value = null;
	if (globalProps.containsKey(key)) {
	    value = (String) globalProps.get(key);
	} else {
	    System.err.println("Key \"" + key + "\" not found! ");
	}
	return value;
    }
    
    public static String getString(String key, String defaultValue) {
	if (key == null || defaultValue == null) {
	    throw new IllegalArgumentException("Key=" + key + ", defaultValue"
		    + defaultValue);
	}
	String value = null;
	if (globalProps.containsKey(key)) {
	    value = (String) globalProps.get(key);
	} else {
	    value = defaultValue;
	    System.err.println("Key " + key
		    + " not found! Used value by default [" + defaultValue  + "]!");
	}
	return value;
    }
    
    public static int getInteger(String key) {
	if (key == null) {
	    throw new IllegalArgumentException("Key=" + key);
	}
	int value = 0;
	if (globalProps.containsKey(key)) {
	    String str = (String) globalProps.get(key);
	    Integer integer = new Integer(str);
	    value = integer.intValue();
	} else {
	    System.err.println("Key \"" + key + "\" not found!");
	}
	return value;
    }

    public static int getInteger(String key, int defaultValue) {
	if (key == null || defaultValue < 0) {
	    throw new IllegalArgumentException("Key=" + key + ", defaultValue"
		    + defaultValue);
	}

	int value = 0;
	if (globalProps.containsKey(key)) {
	    String str = (String) globalProps.get(key);
	    Integer integer = new Integer(str);
	    value = integer.intValue();
	} else {
	    value = defaultValue;
	    System.err.println("Key " + key
		    + " not found! Used value by default [" + defaultValue + "]!");
	}
	return value;
    }
    
    public static void main(String[] args) {
	
	String javaVersion = System.getProperty("java.version");
	if (javaVersion.compareTo("1.5") < 0) {
	    System.err.println("You are running Java version " + javaVersion + '.');
	    System.err.println("Fruit requires Java 1.5 or later.");
	    System.exit(1);
	}

	loadProperties(new File(PROPERTIES_FILE_NAME));
	
	SwingUtilities.invokeLater(new Runnable() {
	    public void run() {
		Window window = new Window();
		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		window.setLocationRelativeTo(null);
		window.setVisible(true);
	    }
	});
    }
}